package cn.xianshu.user.controller;


import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.user.domain.Logininfo;
import cn.xianshu.user.query.LogininfoQuery;
import cn.xianshu.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfoController")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    @GetMapping("/{id}")
    public Logininfo findOne(@PathVariable("id") Long id){
        return logininfoService.findById(id);
    }

    @GetMapping
    public List<Logininfo> findAll(){
        return logininfoService.findAll();
    }

    @DeleteMapping("/{id}")
    public AxiosResult delete(@PathVariable("id") Long id){
        try {
            logininfoService.delete(id);
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("删除失败，请重试！");
        }
    }

    @PutMapping
    public AxiosResult addAndUpdate(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId() == null){
                logininfoService.insert(logininfo);
            } else {
                logininfoService.update(logininfo);
            }
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("操作失败，请重试！");
        }
    }

    @PostMapping
    public PageList<Logininfo> queryList(@RequestBody LogininfoQuery query){
        return logininfoService.queryList(query);
    }
}
